<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>行业信息 信息管理</title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <link rel="stylesheet" th:href="@{/layui/css/font.css}">
    <link rel="stylesheet" th:href="@{/layui/css/xadmin.css}">
    <script charset="utf-8" th:src="@{/layui/lib/layui/layui.js}"></script>
    <script th:src="@{/layui/js/xadmin.js}" type="text/javascript"></script>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <div class="layui-col-space5 layui-form">
                        <div class="layui-input-inline layui-show-xs-block">
                            <label>
                                <input autocomplete="off" class="layui-input" id="industryName"
                                       name="industryName" placeholder="请输入行业名称" type="text">
                            </label>
                        </div>
                        <div class="layui-input-inline layui-show-xs-block">
                            <button class="layui-btn icon-btn" id="reset">
                                <i class="layui-icon">&#xe669;</i>
                            </button>
                            <button class="layui-btn" data-type="reload">
                                <i class="layui-icon">&#xe615;</i>
                            </button>
                        </div>
                    </div>
                </div>
                <div class="layui-card-header">
                    <button class="layui-btn layui-btn-danger" id="delAll">
                        <i class="layui-icon"></i>批量删除
                    </button>
                    <button class="layui-btn"
                            onclick="xadmin.open('添加行业分类信息','/admin_set_industry_message_addView',null,null, false)">
                        <i class="layui-icon"></i>添加
                    </button>
                </div>
                <div class="layui-card-body ">
                    <table class="layui-table layui-form" id="industryMessage"
                           lay-filter="table">
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use([ 'form', 'layer', 'table' ], function() {
        const table = layui.table, $ = layui.$;
        //执行一个laydate实例
        table.render({
            elem : '#industryMessage', //指定元素
            url : '/admin_set_industry_message_list',
            id : 'list',
            page : {
                layout : [ 'limit', 'count', 'prev', 'page', 'next', 'skip' ],
                groups : 1,
                first : false,
                last : false
            },
            cols : [ [ //表头
                {type : 'checkbox'},
                {field : 'industryName', title : '行业名称',  width: 1052, minWidth : 1052, align: "center", sort : true},
                {fixed : 'right', title : '操作', toolbar : '#barDemo', align:"center", width : 120}
            ] ]
        });
        // 查询信息
        const active = {
            reload: function(){
                const industryName = $('#industryName');
                //执行重载
                table.reload('list', {
                    page: {
                        //重新从第 1 页开始
                        curr: 1
                    }
                    ,where: {
                        industryName: industryName.val(),
                    }
                }, 'data');

            }
        };
        $('.layui-card-body .layui-btn').on('click', function(){
            const type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        //重置按钮
        $("#reset").on("click",function () {
            location.reload();
        });
        //监听行工具事件
        table.on('tool(table)', function(obj){
            switch (obj.event) {
                case 'edit':
                    xadmin.open('修改行业分类信息','/admin_set_industry_message_editView?id='+obj.data.id,null,null,false)
                    break;
                case 'del':
                    layer.confirm('真的删除该数据吗?', function(index){
                        $.ajax({
                            url: '/admin_set_industry_message_delete?id='+obj.data.id,
                            type: 'post',
                            success: function (res) {
                                if (res.code === 0) {
                                    //弹出提示，并在1秒后进行跳转
                                    layer.alert("删除成功", {
                                        icon : 6
                                    }, function() {
                                        obj.del();
                                        layer.close(index);
                                        location.reload();
                                        return false;
                                    });
                                } else {
                                    layer.msg("删除失败:" + res.msg);
                                    return false;
                                }
                            }
                        })
                    });
                    break;
            }
        });
        //批量删除
        $("#delAll").on('click',function(){
            //获取选中状态
            let checkStatus = table.checkStatus('list');
            //获取选中数量
            let selectCount = checkStatus.data.length;
            if(selectCount === 0){
                layer.msg('批量删除至少选中一项数据',function(){});
                return false;
            }
            layer.confirm('真的要删除选中的项吗？',function(index){
                layer.close(index);
                index = layer.load(1, {shade: [0.1,'#fff']});

                let str="";
                for(let i = 0; i < selectCount; i ++){
                    str = str + "," + checkStatus.data[i].id;
                }
                $.ajax({
                    type:'post',
                    data:{"arrayId": str},
                    url:'/admin_set_industry_message_delete_select',
                    success:function(res){
                        if(res.code === 0){
                            //弹出提示，并在1秒后进行跳转
                            layer.alert("删除成功", {
                                icon : 6
                            }, function() {
                                layer.close(index);
                                location.reload();
                                return false;
                            });
                        } else {
                            layer.msg("删除失败:" + res.msg);
                            return false;
                        }
                    }
                });
            })
        });
    });
</script>
<script id="barDemo" type="text/html">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
</body>
</html>